﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Skymate
{
    using Skymate.Commands;

    public partial class LightColorStateDialog : Form
    {
        public IList<LightColorState> LightColorStates { get;private set; }

        public LightColorStateDialog(List<LightColor> colors)
        {
            InitializeComponent();

            this.lv_light_colors.BeginUpdate();

            for (int i = 0; i < colors.Count; i++)
            {
                this.AddLightColorState(new LightColorState() { Index = i + 1, LightColor = colors[i] });
            }
            
            this.lv_light_colors.EndUpdate();
        }

        private void btn_new_light_color_Click(object sender, EventArgs e)
        {
            var item = this.AddLightColorState(new LightColorState()
                                                   {
                                                       Index = 
                                                       this.lv_light_colors.Items.Count+1
                                                   });
            item.Selected = true;
        }


        private ListViewItem AddLightColorState(LightColorState state)
        {
            ListViewItem item = new ListViewItem();
            var values = state.ToStrings();
            item.Text = values[0];
            for (int i = 1; i < values.Length; i++)
            {
                item.SubItems.Add(values[i]);
            }

            item.Tag = state;
            this.lv_light_colors.Items.Add(item);

            return item;
        }

        private void lv_light_colors_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            
            this.propertyGrid1.SelectedObject = e.Item.Tag as LightColorState;

        }

        private void btn_removve_Click(object sender, EventArgs e)
        {
            if (this.lv_light_colors.SelectedItems.Count <= 0)
            {
                MessageBox.Show("没有选中行", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var item = this.lv_light_colors.SelectedItems[0];
            item.Remove();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            this.LightColorStates = new List<LightColorState>();
            foreach (ListViewItem item in this.lv_light_colors.Items)
            {
                this.LightColorStates.Add(item.Tag as LightColorState);
            }

            this.DialogResult=DialogResult.OK;
            this.Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult=DialogResult.Cancel;
            this.Close();
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            var data = this.propertyGrid1.SelectedObject as LightColorState;

            this.lv_light_colors.BeginUpdate();
            var selected = this.lv_light_colors.SelectedItems[0];

            var values = data.ToStrings();
            for (int i = 0; i < values.Length; i++)
            {
                selected.SubItems[i].Text = values[i];
            }

            this.lv_light_colors.EndUpdate();
        }
    }

    public class LightColorState
    {
        [DisplayName("灯组号")]
        [ReadOnly(true)]
        public int Index { get; set; }

        [DisplayName("灯色")]
        public LightColor LightColor { get; set; }

        public string[] ToStrings()
        {
            IList<string> result = new List<string>();

            result.Add(Index.ToString());
            result.Add(LightColor.ToString());

            return result.ToArray();
        }
    }
}
